package com.ternnetwork.core.access.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.commons.utils.JsonUtils;
import com.ternnetwork.core.accesscontroll.dto.RoleQueryDto;
import com.ternnetwork.core.accesscontroll.entity.Role;
import com.ternnetwork.core.accesscontroll.service.RoleService;



@Controller
@RequestMapping("/access/role/*")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	
	 @RequestMapping("/getOne")
     public @ResponseBody BaseResponse getOne(Long id) {
     	 return roleService.getOne(id);
     }

	 @PreAuthorize("hasAnyAuthority('ACCESS_ROLE_ADD')")
	 @RequestMapping("/add")
	 public @ResponseBody BaseResponse add(String requestJson) throws Exception {
       	 BaseResponse baseResponse=new BaseResponse();
    	 Role t=JsonUtils.jsonToObjectThrowsException(requestJson, Role.class);
		 baseResponse=roleService.add(t);
		 return baseResponse;
    }

	 @PreAuthorize("hasAnyAuthority('ACCESS_ROLE_UPDATE')")
	 @RequestMapping("/update")
     public @ResponseBody BaseResponse update(String requestJson)  throws Exception{
       	 BaseResponse baseResponse=new BaseResponse();
    	 Role t=JsonUtils.jsonToObjectThrowsException(requestJson, Role.class);
		 baseResponse=roleService.update(t);
		 return baseResponse;
     }

	 @PreAuthorize("hasAnyAuthority('ACCESS_ROLE_QUERY')")
	 @RequestMapping("/findByPage")
     public @ResponseBody BaseResponse findByPage(@PageableDefault Pageable pageable,RoleQueryDto dto) {
      	 return roleService.findByPage(pageable, dto);
     }
    
	 @RequestMapping("/findAll")
     public @ResponseBody BaseResponse findAll(RoleQueryDto dto) {
    	 return roleService.findAll(dto);
     }

	 @PreAuthorize("hasAnyAuthority('ACCESS_ROLE_DELETE')")
	 @RequestMapping("/deleteById")
	 //清空用户菜单缓存
	 @CacheEvict(value="userMenu", allEntries=true)
     public @ResponseBody BaseResponse deleteById(Long id) {
    	 return roleService.deleteById(id);
     }
	 
	 @RequestMapping("/findAllByCategory")
     public @ResponseBody BaseResponse findAllByCategory(String  category) {
    	 return roleService.findAllByCategory(category);
     }
	 
	 @RequestMapping("/findByCode")
     public @ResponseBody BaseResponse findByCode(String  code) {
    	 return roleService.findByCode(code);
     }
	
	

}
